package cate.game.pvp.throne.data.ko;

import cate.common.table.d.GDFight;
import cate.common.table.d.GDThrone;
import cate.game.play.proce.play.PlayPO;
import cate.game.role.RoleSimpleBase;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

@NoteClass("王座争霸-淘汰赛-单个对阵信息")
@Slf4j
public class TBKoMatch {

	@NoteField("对阵分组id")
	public int matchGroup;

	@NoteField("a方简要信息")
	public RoleSimpleBase a;

	@NoteField("b方简要信息")
	public RoleSimpleBase b;

	@NoteField("胜利方")
	public byte winner;

	@NoteField("录像信息")
	public List<TBReplayInfo> replayInfos = new ArrayList<>();

	public TBKoMatch() {
	}

	public TBKoMatch(int matchGroup) {
		this.matchGroup = matchGroup;
	}

	public void initialize() {
		if (a == null) {
			a = new RoleSimpleBase();
		}
		if (b == null) {
			b = new RoleSimpleBase();
		}
		if (replayInfos == null) {
			replayInfos = new ArrayList<>();
		}
	}

	public boolean containRole(String roleUid) {
		if (StringUtils.equals(a.uid, roleUid)) {
			return true;
		}
		if (StringUtils.equals(b.uid, roleUid)) {
			return true;
		}
		return false;
	}



	@JsonIgnore
	public boolean isEmpty() {
		return StringUtils.isEmpty(a.uid) && StringUtils.isEmpty(b.uid);
	}

	public boolean canBet(){
		return StringUtils.isNotEmpty(a.uid) && StringUtils.isNotEmpty(b.uid);
	}

	@JsonIgnore
	public RoleSimpleBase getWinnerInfo() {
		//未分胜负也算a赢
		if (winner == GDFight.Team.NONE) {
			return a;
		}
		if (winner == GDFight.Team.A) {
			return a;
		} else {
			return b;
		}
	}

	@JsonIgnore
	public RoleSimpleBase getLoserInfo() {
		//未分胜负也算a赢
		if (winner == GDFight.Team.NONE) {
			return b;
		}
		if (winner == GDFight.Team.A) {
			return b;
		} else {
			return a;
		}
	}


	public void calWinner(int winTime) {
		int winTimeA = getWinTime(GDFight.Team.A);
		if (winTimeA >= winTime) {
			this.winner = GDFight.Team.A;
			return;
		}
		int winTimeB = getWinTime(GDFight.Team.B);
		if (winTimeB >= winTime) {
			this.winner = GDFight.Team.B;
			return;
		}
		this.winner = GDFight.Team.NONE;
	}

	@JsonIgnore
	public int getWinTime(byte team) {
		int winTime = 0;
		for (TBReplayInfo replayInfo : replayInfos) {
			if (replayInfo.winner == team) {
				winTime++;
			}
		}
		return winTime;
	}

	@JsonIgnore
	public int getWinTime(String masterIdHex) {
		byte team = findTeam(masterIdHex);
		int winTime = 0;
		for (TBReplayInfo replayInfo : replayInfos) {
			if (replayInfo.winner == team) {
				winTime++;
			}
		}
		return winTime;
	}

	public byte findTeam(String masterIdHex){
		if(StringUtils.equals(a.uid, masterIdHex)){
			return GDFight.Team.A;
		}
		if(StringUtils.equals(b.uid, masterIdHex)){
			return GDFight.Team.B;
		}
		return GDFight.Team.NONE;
	}

	public void addReplayInfo(long replayId, PlayPO play) {
		TBReplayInfo info = new TBReplayInfo();
		info.replayUid = replayId;
		info.duration = play.endTime - play.startTime;
		info.winner = play.winner;
		this.replayInfos.add(info);
	}

	@JsonIgnore
	public boolean isOver(){
		return winner == GDFight.Team.A || winner == GDFight.Team.B;
	}


	public void setPlayer(byte team, RoleSimpleBase roleSimpleBase) {
		switch (team) {
			case GDFight.Team.A:
				this.a = roleSimpleBase;
				break;
			case GDFight.Team.B:
				this.b = roleSimpleBase;
				break;
			default:
				log.error("王座争霸匹配信息设置有误 team = {}", team);
		}
	}

	@JsonIgnore
	public RoleSimpleBase getPlayerInfoByTeam(byte team){
		if (team == GDFight.Team.A) {
			return a;
		} else if (team == GDFight.Team.B) {
			return b;
		}
		return null;
	}
}
